<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
	<head>
		<jsp:include page="../inc.jsp"></jsp:include>
		<script type="text/javascript" src="../My97DatePicker/WdatePicker.js"></script>
		<script type="text/javascript" charset="utf-8">
	var datagrid;
	var repertory = '${sessionInfo.repertoryIds}';
	var auth = "${auths}";
	var buttons = [];
	var confirm = false;
	$(function() {
		$('form input').bind('keyup', function(event) {/* 增加回车提交功能 */
			if (event.keyCode == '13') {
				datagrid.datagrid('load', sy.serializeObject($('#searchForm')));
			}
		});
		if(auth=="all"){
			buttons.push({text : '处理',iconCls : 'icon-edit',handler : function() {edit();}});
			buttons.push('-');
			buttons.push({text : '取消选中',iconCls : 'icon-undo',handler : function() {datagrid.datagrid('clearSelections');datagrid.datagrid('unselectAll');}});
			buttons.push('-');
			confirm=true;
		}else if(auth!=""){
			var auths = auth.split(",");
			if(auths.length>0){
				for(var i=0;i<auths.length;i++){
					if(auths[i]=="KCGL_KCFH_EDIT"){
						buttons.push({text : '处理',iconCls : 'icon-edit',handler : function() {edit();}});
						buttons.push('-');
					}else if(auths[i]=="KCGL_KCFH_CONFIRM"){
						confirm = true;
					}
				}
				buttons.push({text : '取消选中',iconCls : 'icon-undo',handler : function() {datagrid.datagrid('clearSelections');datagrid.datagrid('unselectAll');}});
				buttons.push('-');
			}
		}
		datagrid = $('#datagrid').datagrid({
			url : 'goodsDeliverAction!datagridForDealer.html',
			title : '相关发货单列表',
			iconCls : 'icon-save',
			pagination : true,
			pagePosition : 'bottom',
			pageSize : 10,
			pageList : [ 10, 20, 30, 40 ],
			fit : true,
			fitColumns : false,
			nowrap : false,
			border : false,
			idField : 'id',
			sortName : 'serial',
			sortOrder : 'desc',
			checkOnSelect : false,
			selectOnCheck : false,
			singleSelect : true,
			frozenColumns : [ [ {
				title : '编号',
				field : 'id',
				width : 120,
				sortable : true,
				checkbox : true
			}] ],
			columns : [ [  {
				title : '日期',
				field : 'date',
				width : 100,
				formatter : function(value, row, index) {
					return value.substring(0,10);
				}
			}, {
				title : '单号',
				field : 'serial',
				width : 120
			},{
				title : '操作员',
				field : 'uname',
				width : 100
			},  {
				title : '发货仓库ID',
				field : 'orid',
				width : 100,
				hidden : true
			}, {
				title : '发货仓库',
				field : 'orname',
				width : 100,
				align:'center'
			},{
				title : '发货总数',
				field : 'num',
				width : 80,
				align:'center'
			},{
				title : '发出确认',
				field : 'outAction',
				width : 100,
				align:'center',
				formatter : function(value, row, index) {
					if (row.isOutConfirm) {
						return '确认人：'+row.ocuname;
					} else if(repertory!='null'){
						if(repertory == row.orid){
							if(confirm){
								return '<img onclick="outConfirm(\''+row.id+'\');" style="cursor:pointer;" src="${pageContext.request.contextPath}/style/images/extjs_icons/tick.png"/>';
							}else{
								return '';
							}
						}else{
							return '发货未确认'; 
						}
					}else{
						if(confirm){
							return '<img onclick="outConfirm(\''+row.id+'\');" style="cursor:pointer;" src="${pageContext.request.contextPath}/style/images/extjs_icons/tick.png"/>';
						}else{
							return '';
						}
					}
				}
			} ,{
				title : '收货处理',
				field : 'inAction',
				width : 100,
				align:'center',
				formatter : function(value, row, index) {
					if(row.isOutConfirm){
						if (row.isInConfirm) {
							return '确认人：'+row.icuname;
						} else if(repertory!='null'){
							if(repertory == row.irid){
								return '<img onclick="inConfirm(\''+row.id+'\');" style="cursor:pointer;" src="${pageContext.request.contextPath}/style/images/extjs_icons/tick.png"/>';
							}else{
								return '';
							}
						}else{
							return '<img onclick="inConfirm(\''+row.id+'\');" style="cursor:pointer;" src="${pageContext.request.contextPath}/style/images/extjs_icons/tick.png"/>';
						}
					}else{
						return '';
					}
				}
			} ] ],
			toolbar : [ {
				text : '处理',
				iconCls : 'icon-edit',
				handler : function() {
					edit();
				}
			}, '-', {
				text : '取消选中',
				iconCls : 'icon-undo',
				handler : function() {
					datagrid.datagrid('clearSelections');
					datagrid.datagrid('unselectAll');
				}
			}, '-']
		});
		
		var agencyid = $('#searchForm').find('input[name=agencyid]');
			var agencyidCombobox = agencyid.combobox({
				url : '${pageContext.request.contextPath}/dls/agencyAction!noAuth_MyAgencyJson.html',
				valueField : 'id', 
				textField : 'username',
				multiple : false,
				editable : false,
				panelHeight : 'auto'
			});
			
			var orid = $('#searchForm').find('input[name=orid]');
			var oridCombobox = orid.combobox({
				url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
				valueField : 'id',
				textField : 'name',
				multiple : false,
				editable : false,
				panelHeight : 'auto'
			});

	});
	
	function outConfirm(id){
		parent.sy.messagerConfirm('请确认', '您要确认当前所选项目？', function(r) {
			if (r) {
				$.ajax({
					url : '${pageContext.request.contextPath}/kcgl/goodsDeliverAction!outConfirm.html',
					data : {id:id},
					dataType : 'json',
					success : function(d) {
						datagrid.datagrid('load');
						datagrid.datagrid('unselectAll');
						parent.sy.messagerShow({
							title : '提示',
							msg : d.msg
						});
					}
				});
			}
		});
	}
	
	function inConfirm(id){
		parent.sy.messagerConfirm('请确认', '您要确认当前所选项目？', function(r) {
			if (r) {
				$.ajax({
					url : '${pageContext.request.contextPath}/kcgl/goodsDeliverAction!inConfirm.html',
					data : {id:id},
					dataType : 'json',
					success : function(d) {
						datagrid.datagrid('load');
						datagrid.datagrid('unselectAll');
						parent.sy.messagerShow({
							title : '提示',
							msg : d.msg
						});
					}
				});
			}
		});
	}

	
	function edit() {
		var rows = datagrid.datagrid('getSelections');
		//var node = {id:'KCGL_KCDB_BJ',text:'编辑库存调拨单',state:'open',attributes:{url:'/kcgl/goodsTransferItemAction!goodsTransferItemEdit.html'}};
		if (rows.length == 1) {
			var oid = rows[0].id;
			alert(oid);
			var node = {id:'KCGL_KCDB_BJ',text:'处理收货单',state:'open',attributes:{url:'/kcgl/goodsDeliverItemAction!goodsReceiveEdit.html?model.id='+oid}};
			if (parent.centerTabs.tabs('exists', node.text)) {
				parent.centerTabs.tabs('select', node.text);
				parent.refreshTab2(node.text,'/kcgl/goodsDeliverItemAction!goodsReceiveEdit.html?model.id='+oid);				
			}else{
				parent.addTab(node);
			}
		} else if (rows.length > 1) {
			parent.sy.messagerAlert('提示', '同一时间只能查看一条记录！', 'error');
		} else {
			parent.sy.messagerAlert('提示', '请选择要查看的记录！', 'error');
		}
	}
	
	function _search() {
		datagrid.datagrid('load', sy.serializeObject($('#searchForm')));
	}
	
	function cleanSearch() {
		datagrid.datagrid('load', {});
		$('#searchForm input').val('');
	}
</script>
	</head>
	<body class="easyui-layout" data-options="fit:true">
		
		<div data-options="region:'north',border:false,title:'搜索条件'" style="height: 115px; overflow: hidden;">
			<form id="searchForm">
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 80px;">开始日期</th>
						<td style="width: 200px;"><input name="beginDate" class="Wdate" onclick="WdatePicker()" style="width: 200px;" /></td>
						<th style="width: 80px;">截止日期</th>
						<td style="width: 200px;"><input name="endDate" class="Wdate" onclick="WdatePicker()" style="width: 200px;" /></td>
					</tr>
					<tr>
						<!-- <th>发出仓库</th>
						<td><input name="orid" style="width: 200px;" /></td> -->
						<th>发货代理商</th>
						<td><input name="agencyid" style="width: 200px;" /></td>
						<td colspan="6" align="right" style="text-align:center">
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="_search();">搜索</a>
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="cleanSearch();">取消</a>
						</td>
					</tr>
					<!-- <tr>
						<td colspan="6" align="right" style="text-align:center">
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="_search();">搜索</a>
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="cleanSearch();">取消</a>
						</td>
					</tr> -->
				</table>
			</form>
		</div>
		
		<div data-options="region:'center',border:false" style="overflow: hidden;">
			<table id="datagrid"></table>
		</div>
		
		<div id="menu" class="easyui-menu" style="width:120px;display: none;">
			<k:security key="KCGL_KCDB_ADD">
				<div onclick="append();" data-options="iconCls:'icon-add'">增加</div>
			</k:security>
			<k:security key="KCGL_KCDB_DELETE">
				<div onclick="remove();" data-options="iconCls:'icon-remove'">删除</div>
			</k:security>
		</div>
	</body>
</html>